﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_ProviderTrailer : DockContent
    {
        private DGBY.BLL.TCCompDataTab m_bllTCCompDataTab = new DGBY.BLL.TCCompDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectTCCompDataTabID = 0;
        private Dictionary<string, string> dicQueryCondition = new Dictionary<string, string>();
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        private bool IsDecimal(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"([1-9]+[0-9]*(\.[0-9]+))|(0(\.[0-9]+))");
            return bRet;
        }

        private bool IsNum(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"^[1-9]+\d*$");
            return bRet;
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        public bool IsDecimal(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsDecimal(TextBox.Text) && !IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNum(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNull(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }
        public Frm_ProviderTrailer()
        {
            InitializeComponent();
        }
        private void Frm_ProviderTrailer_Load(object sender, EventArgs e)
        {
            InitTrailerQueryCondition();
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
            }
        }

        private void InitTrailerQueryCondition()
        {
            dicQueryCondition.Add("拖车公司名称", "TCCompName");
            dicQueryCondition.Add("拖车公司简称", "TCCompNameJX");
            dicQueryCondition.Add("拖车公司电话", "TCCompTel");
            dicQueryCondition.Add("拖车公司传真", "TCCompFax");
            dicQueryCondition.Add("经理姓名", "ManagerName");
            dicQueryCondition.Add("经理电话", "ManagerTel");
            dicQueryCondition.Add("备注", "Note");
            this.comboBoxQueryCondition.SelectedIndex = 0;
            this.radioButtonValueIs.Select();
        }
        private void buttonTrailerAddr_Click(object sender, EventArgs e)
        {
            Frm_ProviderTrailerAddr frm_ProviderTrailerAddr = new Frm_ProviderTrailerAddr();
            frm_ProviderTrailerAddr.StartPosition = FormStartPosition.CenterScreen;
            frm_ProviderTrailerAddr.ShowDialog();
        }

        private void buttonTrailerContactPerson_Click(object sender, EventArgs e)
        {
            Frm_ProviderTrailerConP frm_ProviderTrailerConP = new Frm_ProviderTrailerConP();
            frm_ProviderTrailerConP.StartPosition = FormStartPosition.CenterScreen;
            frm_ProviderTrailerConP.ShowDialog();
        }

        private void buttonTrailerConsume_Click(object sender, EventArgs e)
        {
            Frm_ProviderTrailerConsume frm_ProviderTrailerConsume = new Frm_ProviderTrailerConsume();
            frm_ProviderTrailerConsume.StartPosition = FormStartPosition.CenterScreen;
            frm_ProviderTrailerConsume.ShowDialog();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewTrailerList.DataSource = null;
            m_ds = m_bllTCCompDataTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewTrailerList.DataSource = m_ds.Tables[0];
                dataGridViewTrailerList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecords_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewTrailerList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strTCCompDataTabID = this.dataGridViewTrailerList.Rows[this.dataGridViewTrailerList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectTCCompDataTabID = Convert.ToInt32(strTCCompDataTabID, 10);
            cellclickTCComp();
        }

        private void cellclickTCComp()
        {
            DGBY.Model.TCCompDataTab modelTCCompDataTab = m_bllTCCompDataTab.GetModel(m_nSelectTCCompDataTabID);
            if (modelTCCompDataTab != null)
            {
                this.textBoxTrailerName.Text = modelTCCompDataTab.TCCompName;
                this.textBoxTrailerAbbrName.Text = modelTCCompDataTab.TCCompNameJX;
                this.textBoxTrailerFax.Text = modelTCCompDataTab.TCCompFax;
                this.textBoxTrailerPhone.Text = modelTCCompDataTab.TCCompTel;
                this.textBoxManagerName.Text = modelTCCompDataTab.ManagerName;
                this.textBoxManagerPhone.Text = modelTCCompDataTab.ManagerTel;
                this.richTextBoxNote.Text = modelTCCompDataTab.Note;
                this.textBoxQiChuRemainJinE.Text = modelTCCompDataTab.QiChuRemainJinE.ToString();
                this.textBoxTransPrice.Text = modelTCCompDataTab.TransPrice.ToString();
                

            }
            else
            {
                this.textBoxTrailerName.Text = "";
                this.textBoxTrailerAbbrName.Text = "";
                this.textBoxTrailerFax.Text = "";
                this.textBoxTrailerPhone.Text = "";
                this.textBoxManagerName.Text = "";
                this.textBoxManagerPhone.Text = "";
                this.richTextBoxNote.Text = "";
            }
        }

        private void dataGridViewTrailerList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewTrailerList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxQiChuRemainJinE))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxTransPrice))
                    return;
                DGBY.Model.TCCompDataTab modelTCCompDataTab = new DGBY.Model.TCCompDataTab();

                modelTCCompDataTab.TCCompName = this.textBoxTrailerName.Text;
                modelTCCompDataTab.TCCompNameJX = this.textBoxTrailerAbbrName.Text;
                modelTCCompDataTab.TCCompFax = this.textBoxTrailerFax.Text;
                modelTCCompDataTab.TCCompTel = this.textBoxTrailerPhone.Text;
                modelTCCompDataTab.ManagerName = this.textBoxManagerName.Text;
                modelTCCompDataTab.ManagerTel = this.textBoxManagerPhone.Text;
                modelTCCompDataTab.Note = this.richTextBoxNote.Text;
                modelTCCompDataTab.QiChuRemainJinE = Convert.ToDecimal(this.textBoxQiChuRemainJinE.Text);
                modelTCCompDataTab.TransPrice = Convert.ToDecimal(this.textBoxTransPrice.Text);

                int nResult = m_bllTCCompDataTab.Add(modelTCCompDataTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", modelTCCompDataTab.TCCompName);

                    m_nSelectTCCompDataTabID = m_bllTCCompDataTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllTCCompDataTab.GetModel(m_nSelectTCCompDataTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxQiChuRemainJinE))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxTransPrice))
                    return;
                DGBY.Model.TCCompDataTab modelTCCompDataTab = m_bllTCCompDataTab.GetModel(m_nSelectTCCompDataTabID);
               // modelTCCompDataTab.TCCompDataTabID = m_nSelectTCCompDataTabID;
                modelTCCompDataTab.TCCompName = this.textBoxTrailerName.Text;
                modelTCCompDataTab.TCCompNameJX = this.textBoxTrailerAbbrName.Text;
                modelTCCompDataTab.TCCompFax = this.textBoxTrailerFax.Text;
                modelTCCompDataTab.TCCompTel = this.textBoxTrailerPhone.Text;
                modelTCCompDataTab.ManagerName = this.textBoxManagerName.Text;
                modelTCCompDataTab.ManagerTel = this.textBoxManagerPhone.Text;
                modelTCCompDataTab.Note = this.richTextBoxNote.Text;
                modelTCCompDataTab.QiChuRemainJinE = Convert.ToDecimal(this.textBoxQiChuRemainJinE.Text);
                modelTCCompDataTab.TransPrice = Convert.ToDecimal(this.textBoxTransPrice.Text);
                m_bllTCCompDataTab.Update(modelTCCompDataTab);

                Frm_Main.Log2DB(this.Text, "修改", modelTCCompDataTab.TCCompName);

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllTCCompDataTab.GetModel(m_nSelectTCCompDataTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllTCCompDataTab.Delete(m_nSelectTCCompDataTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickTCComp();
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            string strTemp = this.comboBoxQueryCondition.SelectedItem.ToString();

            strWhere = dicQueryCondition[strTemp] + " LIKE " + "'%" + this.textBoxValueIs.Text + "%'";


            //string strOrderBy = "\nORDER BY FactoryCode";
            //strWhere = strWhere + strOrderBy;
            DataSet ds = new DataSet();
            ds = m_bllTCCompDataTab.GetList(strWhere);

            dataGridViewTrailerList.DataSource = null;
            dataGridViewTrailerList.DataSource = ds.Tables[0];
            if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
            {
                this.dataGridViewTrailerList.DataSource = ds.Tables[0];
                this.dataGridViewTrailerList.Columns[0].Visible = false;
            }
            else
            {
                this.dataGridViewTrailerList.DataSource = null;
            }

            return;
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewTrailerList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "TrailerCompInfo.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}